import { EventEmitter } from 'events' //引入events事件
import Cookies from 'js-cookie'

export const stepState = {
  '0': '填写基本资料',
  '1': '填写安全信息服务',
  '2': '发布完成',
}

export const userLevel = ['普通卖家', '金牌签约卖家', '钻石签约卖家', '核心优质商户', null, '普通卖家']

export const bus = new EventEmitter()

/**
 * 动画垂直滚动到页面指定位置
 * @param { Number } currentY 当前位置
 * @param { Number } targetY 目标位置
 */
export function scrollAnimation(currentY, targetY) {
  // 获取当前位置方法
  // const currentY = document.documentElement.scrollTop || document.body.scrollTop

  // 计算需要移动的距离
  let needScrollTop = targetY - currentY
  let _currentY = currentY
  setTimeout(() => {
    // 一次调用滑动帧数，每次调用会不一样
    const dist = Math.ceil(needScrollTop / 10)
    _currentY += dist
    window.scrollTo(_currentY, currentY)
    // 如果移动幅度小于十个像素，直接移动，否则递归调用，实现动画效果
    if (needScrollTop > 10 || needScrollTop < -10) {
      scrollAnimation(_currentY, targetY)
    } else {
      window.scrollTo(_currentY, targetY)
    }
  }, 1)
}
// 创造随机数据
export function creatRandowData() {
  let phoneNumFont = ['139', '138', '137', '136', '135', '134', '159', '158', '157', '150', '151', '152', '188', '187', '182', '183', '184', '178', '130', '131', '132', '156', '155', '186', '185', '176', '133', '153', '189', '180', '181', '177']
  let orderArr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  let arry = []
  for (let i = 0; i < 60; i++) {
    let key = parseInt(Math.random() * phoneNumFont.length)
    let randowObj = {}
    let phoneNumber = ''
    let orderString = ''
    for (var j = 0; j < 8; j++) {
      phoneNumber += Math.floor(Math.random() * 10)
    }
    for (var g = 0; g < 11; g++) {
      var index = Math.round(Math.random() * (orderArr.length - 1))
      orderString += orderArr[index]
    }
    randowObj['phoneNumber'] = phoneNumFont[key] + phoneNumber
    randowObj['orderMoneny'] = ((Math.random() * 49 + 5) / 10).toFixed(1)
    randowObj['orderNumber'] = '13' + orderString
    arry.push(randowObj)
  }
  return arry
}
// 读取cookie
export function getCookie(name) {
  var arr,
    reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
  if ((arr = document.cookie.match(reg))) return unescape(arr[2])
  else return null
}

//写cookies
export function setCookie(name, value, minutes) {
  var exp = new Date()
  exp.setTime(exp.getTime() + minutes * 60 * 1000)
  document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
}

const TokenKey = 'next_template_token'

export function getToken() {
  return Cookies.get(TokenKey) || null
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

/**
 * 获取地址栏参数
 * @param {*} name
 * @returns
 */
export function QueryString(name) {
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  let r = window.location.search.substr(1).match(reg)
  if (r != null) return decodeURIComponent(r[2])
  return null
}
